package com.boco.fznq.personal;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.boco.fznq.R;
import com.boco.fznq.login.AccountUtils;
import com.boco.fznq.login.LoginActivity;
import com.boco.fznq.login.LoginEvent;
import com.boco.fznq.login.RegisterActivity;
import com.boco.fznq.login.bean.UserBean;
import com.boco.fznq.utils.SPKey;
import com.boco.fznq.utils.SPUtils;
import com.culiu.core.fonts.CustomTextView;
import com.culiu.core.fragment.BaseCoreFragment;
import com.culiu.core.imageloader.CuliuImageLoader;
import com.culiu.core.utils.notification.ToastUtils;
import com.culiu.core.utils.view.ViewUtils;
import com.culiu.core.widget.CustomImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by jiangcheng on 2016/12/12.
 */

public class PersonalFragment extends BaseCoreFragment {


    @BindView(R.id.civ_bg_user)
    CustomImageView mCivBgUser;
    @BindView(R.id.iv_personal_login)
    CustomImageView mIvPersonalLogin;
    @BindView(R.id.fl_user)
    FrameLayout mFlUser;
    @BindView(R.id.tv_login)
    CustomTextView mTvLogin;
    @BindView(R.id.tv_register)
    CustomTextView mTvRegister;
    @BindView(R.id.ll_login)
    LinearLayout mLlLogin;
    @BindView(R.id.tv_username)
    CustomTextView mTvUsername;
    @BindView(R.id.rl_user_header)
    RelativeLayout mRlUserHeader;
    @BindView(R.id.modify_password)
    RelativeLayout mModifyPassword;
    @BindView(R.id.check_update)
    RelativeLayout mCheckUpdate;
    @BindView(R.id.about_us)
    RelativeLayout mAboutUs;
    @BindView(R.id.tv_logout)
    TextView mTvLogout;


    Unbinder unbinder;

    private UserBean.UserInfo mUserInfo;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personal, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }



    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initViews();
    }

    private void initViews() {
        if (AccountUtils.isLogin(getActivity())) {
            showLoginView();
        } else {
            showLogoutView();
        }
    }

    private void showLoginView() {
        ViewUtils.setGone(mLlLogin, true);
        ViewUtils.setGone(mTvUsername, false);
        ViewUtils.setGone(mTvLogout, false);
        String phoneNum = (String) SPUtils.get(getActivity(), SPKey.PHONE, "");
        String headerImageUrl = (String) SPUtils.get(getActivity(), SPKey.HEADERIMG, "");
        mTvUsername.setText(phoneNum);
        CuliuImageLoader.getInstance().display(mIvPersonalLogin, headerImageUrl,
                R.drawable.social_default_photo);
    }

    /**
     * 退出登录的view
     */
    private void showLogoutView() {
        SPUtils.clear(getActivity());
        ViewUtils.setGone(mLlLogin, false);
        ViewUtils.setGone(mTvUsername, true);
        ViewUtils.setGone(mTvLogout, true);
        CuliuImageLoader.getInstance().display(mIvPersonalLogin, "",
                R.drawable.social_default_photo);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * EventBus的回调事件
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LoginEvent event) {
        mUserInfo = event.getUserInfo();
        handleLoginAfter();
    }

    private void handleLoginAfter() {
        ViewUtils.setGone(mLlLogin, true);
        ViewUtils.setGone(mTvUsername, false);
        ViewUtils.setGone(mTvLogout, false);
        mTvUsername.setText(mUserInfo.getPhoneNum());
        CuliuImageLoader.getInstance().display(mIvPersonalLogin, mUserInfo.getAvatarUrl(),
                R.drawable.social_default_photo);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_login, R.id.tv_register, R.id.modify_password, R.id.check_update,
            R.id.about_us, R.id.tv_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                getActivity().startActivity(new Intent(getActivity(), LoginActivity.class));
                break;
            case R.id.tv_register:
                getActivity().startActivity(new Intent(getActivity(), RegisterActivity.class));
                break;
            case R.id.modify_password:
                break;
            case R.id.check_update:
                ToastUtils.showShort(getActivity(), "当前已是最新版本!");
                break;
            case R.id.about_us:
                break;
            case R.id.tv_logout:
                showLogoutView();
                ToastUtils.showShort(getActivity(), "退出成功!");
                break;
            default:
                break;
        }
    }
}
